﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.User;

public partial class Logged : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        checkIfIsLogged();
        setProfile();
        setActiveButton();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        Session["ActiveMenuButton"] = lb.ID;
        Response.Redirect(lb.CommandArgument);
    }

    protected void checkIfIsLogged()
    {
        if (Session["Person"] == null || Session["LastAccess"] == null)
            Response.Redirect("~/Login.aspx");

        Int32 personId = (Int32)Session["Person"];
        String lastAccess = (String)Session["LastAccess"];

        DateTime lastAccessTime = DateTime.Parse(lastAccess);
        TimeSpan ts = DateTime.Now.Subtract(lastAccessTime);
        TimeSpan ts2 = new TimeSpan(0, 0, 15, 0, 0);

        if (ts.CompareTo(ts2) > 0)
            Response.Redirect("~/Login.aspx");

        Session["LastAccess"] = DateTime.Now.ToString();
    }

    protected void setProfile()
    {
        Int32 personId = (Int32)Session["Person"];
        Person p = Person.get(personId);

        if (p.getPicturePath() != null)
            ProfileImage.ImageUrl = p.getPicturePath();
        else
            ProfileImage.ImageUrl = "~/Resources/UnknownProfilePicture.png";

        ProfileFirstNameLabel.Text = p.getFirstName();
        ProfileLastNameLabel.Text = p.getLastName();
    }

    protected void setActiveButton()
    {
        string activeMenuButtonID = (string)Session["ActiveMenuButton"];

        if (activeMenuButtonID == null) return;
            //activeMenuButtonID = "menuButton1";

        ContentPlaceHolder menuPlaceHolder = (ContentPlaceHolder)Master.FindControl("menuPlaceHolder");
        LinkButton activeMenuButton = (LinkButton)menuPlaceHolder.FindControl(activeMenuButtonID);

        if (activeMenuButton != null)
        {
            activeMenuButton.BackColor = Color.FromArgb(114, 0, 172);
            activeMenuButton.ForeColor = Color.White;
            activeMenuButton.Visible = true;

            if (activeMenuButton.Attributes["Parent"] != null)
                activeMenuButtonID = activeMenuButton.Attributes["Parent"];

            foreach (Control c in menuPlaceHolder.Controls)
            {
                WebControl wc = c as WebControl;
                if (wc != null) {
                    if (wc.Attributes["Parent"] != null)
                    {
                        if (wc.Attributes["Parent"].Equals(activeMenuButtonID))
                        {
                            wc.Visible = true;
                        }
                    } 
                    else if (wc.ID == activeMenuButtonID)
                        wc.Visible = true;
                }
                
            }
        }

        Session["ActiveMenuButton"] = null;
    }
    protected void toolbarButtonLogout_Click(object sender, ImageClickEventArgs e)
    {
        Session["Person"] = null;
        Response.Redirect("~/Default.aspx");
    }
}
